package controller;

import dao.PhoneDao;
import dao.impl.PhoneDaoImpl;
import entity.Phone;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.SQLException;

/**
 * 执行更新的后端
 */
@WebServlet("/modifyPhone.do")
public class ModifyPhoneServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        int inventory = Integer.parseInt(req.getParameter("inventory"));
        String name = req.getParameter("name");
        String model = req.getParameter("model");
        BigDecimal price = BigDecimal.valueOf(Double.valueOf(req.getParameter("price")));
        Date time = java.sql.Date.valueOf(req.getParameter("time"));

        //建立临时储存
        Phone phone = new Phone(id,inventory,name,model,price,time);
        PhoneDao phoneDao = new PhoneDaoImpl();

        try {
            //执行更新
            phoneDao.updatePhoneById(phone);
            req.getRequestDispatcher("/findAllPhone.do").forward(req,resp);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
